package com.qc.wintrax.activity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.TextView;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GPSInfoActivity extends BaseActivity {
    @Bind(R.id.tv_gps_info_lon)
    TextView tvGpsInfoLon;
    @Bind(R.id.tv_gps_info_lat)
    TextView tvGpsInfoLat;
    @Bind(R.id.tv_gps_info_hei)
    TextView tvGpsInfoHei;
    @Bind(R.id.tv_gps_info_time)
    TextView tvGpsInfoTime;

    private LocationManager locationManager;
    private LocationListener locationListener;
    private Location location = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpsinfo);
        ButterKnife.bind(this);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateView(location);
        // 设置每3秒获取一次GPS的定位信息
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                // 当GPS定位信息发生改变时，更新位置
                updateView(location);
            }

            @Override
            public void onProviderDisabled(String provider) {
                updateView(null);
            }

            @Override
            public void onProviderEnabled(String provider) {
                // 当GPS LocationProvider可用时，更新位置
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.
                        ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                        ActivityCompat.checkSelfPermission(context, Manifest.permission.
                                ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                updateView(locationManager.getLastKnownLocation(provider));
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        });
    }


    public void updateView(Location newLocation) {
        if (newLocation != null) {
//            StringBuilder sb = new StringBuilder();
            tvGpsInfoLon.setText(newLocation.getLongitude() + "");
            tvGpsInfoLat.setText(newLocation.getLatitude() + "");
            tvGpsInfoHei.setText(newLocation.getAltitude() + "");
            tvGpsInfoTime.setText(newLocation.getSpeed() + "");
//            sb.append(newLocation.getBearing() + "");

        } else {
// 如果传入的Location对象为空则清空EditText
        }
    }
}